// 总的路由数据
const routes = [
  {
    name: "home",
  },
  {
    name: "banner",
    children: [
      {
        name: "banner-list",
        children: [
          {
            name: "banner-list-1",
          },
          {
            name: "banner-list-2",
          },
        ],
      },
      {
        name: "banner-add",
      },
    ],
  },
  {
    name: "manager",
    children: [
      {
        name: "manager-list",
      },
    ],
  },
];

// 当前用户的路由权限
const checkedKeys = ["banner", "banner-list", "banner-list-1"];

// 计算得出的有权限的路由数据
const result = [
  {
    name: "banner",
    children: [
      {
        name: "banner-list",
      },
    ],
  },
];

/**
 * 过滤出有权限的路由集合
 * @param {*} routes 总的路由数据
 * @param {*} checkedKeys 权限集合
 */
function filterRoutes(routes, checkedKeys) {
  // 最终返回的结果
  let result = [];

  routes.forEach((route) => {
    // 对 route 拷贝一份
    const obj = { ...route };

    if (checkedKeys.includes(obj.name)) {
      // 判断当前route是否有children
      if (obj.children) {
        obj.children = filterRoutes(obj.children, checkedKeys);
      }

      result.push(obj);
    }
  });

  return result;
}

const arr1 = filterRoutes(routes, checkedKeys);
console.log(arr1);
